<template>
    <div class="user-edit all-height">
        <!--    <div class="drawer-header">-->

        <!--    </div>-->
        <!-- Drawer抽屉 -->
        <!--    <Drawer :title="title" v-model="visible" width="500" draggable :mask-closable="type=='0'">-->
        <div :style="{ maxHeight: maxHeight }" class="drawer-content all-height">
            <div class="drawer-header">
                <div class="cmr16"  v-show="type == '2'">添加区</div>
                <div class="cmr16" v-show="type != '2'">编辑区</div>
                <!-- <Avatar :src="form.avatar" size="large" v-show="type != '2'"/> -->
            </div>
            <Form ref="form" :model="form" :rules="formValidate" label-position="left" :label-width="labelWidth100">
                <Row :gutter="32">
                    <Col span="24">
                        <FormItem label="去向名称" prop="whereManageName">
                            <Input v-model="form.whereManageName" clearable />
                        </FormItem>
                    </Col>
                </Row>
                <Row :gutter="32">
                    <Col span="24">
                        <FormItem label="就业状况" prop="whereManageType">
                            <Select v-model="form.whereManageType" placeholder="请选择" clearable>
                                <Option v-for="item in treeData" :value="item.value" :key="item.id">{{ item.title }}</Option>
                            </Select>
                            <!--              <dict dict="where-manage-type" v-model="form.whereManageType" transfer style="width:100%"/>-->
                        </FormItem>
                    </Col>
                </Row>
                <Row :gutter="32">
                    <Col span="24">
                        <FormItem label="数据值" prop="value">
                            <Input v-model="form.value" clearable />
                        </FormItem>
                    </Col>
                </Row>
            </Form>
        </div>

        <div class="br right-bottom-edit">
            <Button type="text" @click="close">取消</Button>

            <div v-if="type == '1'" style="display: inline">
                <Button v-has="'edit'" type="primary" :loading="submitLoading" @click="submit">提交</Button>
            </div>
            <div v-if="type == '2'" style="display: inline">
                <Button v-has="'add'" type="primary" :loading="submitLoading" @click="submit">提交</Button>
            </div>
        </div>
        <!--    </Drawer>-->
    </div>
</template>

<script>
// 根据你的实际请求api.js位置路径修改
import { addWhereManage, editWhereManage } from '@/api/whereManage.js';
import { getDictDataByType } from '@/api';
// import dict from "@/views/my-components/xboot/dict";
import editor from '@/views/my-components/xboot/editor';

export default {
    name: 'addEdit',
    components: {
        // dict
        editor
    },
    props: {
        value: {
            type: Boolean,
            default: false
        },
        data: {
            type: Object
        },
        type: {
            type: String,
            default: '0'
        }
    },
    data() {
        return {
            labelWidth100: 100,
            treeData: [],
            typeList: '',
            visible: this.value,
            title: '',
            submitLoading: false,
            maxHeight: 510,
            form: {
                // 添加或编辑表单对象初始化数据
                whereManageName: '',
                whereManageType: '',
                value: ''
            },
            // 表单验证规则
            formValidate: {
                whereManageName: [{ required: true, message: '不能为空', trigger: 'change' }],
                whereManageType: [{ required: true, message: '不能为空', trigger: 'change' }],
                value: [{ required: true, message: '不能为空', trigger: 'blur' }]
            }
        };
    },

    methods: {
        init() {
            this.getTypeDataList();
        },
        close() {
            this.$emit('close');
        },
        submit() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    if (this.type == '1') {
                        // 编辑
                        this.submitLoading = true;
                        editWhereManage(this.form).then(res => {
                            this.submitLoading = false;
                            if (res.success) {
                                this.$Message.success('操作成功');
                                this.$emit('on-submit', true);
                                this.visible = false;
                            }
                        });
                    } else {
                        // 添加
                        this.submitLoading = true;
                        addWhereManage(this.form).then(res => {
                            this.submitLoading = false;
                            if (res.success) {
                                this.$Message.success('操作成功');
                                this.$emit('on-submit', true);
                                this.visible = false;
                            }
                        });
                    }
                }
            });
        },
        setCurrentValue(value) {
            if (value === this.visible) {
                return;
            }
            if (this.type == '1') {
                this.title = '编辑';
                this.maxHeight = Number(document.documentElement.clientHeight - 121) + 'px';
            } else if (this.type == '2') {
                this.title = '添加';
                this.maxHeight = Number(document.documentElement.clientHeight - 121) + 'px';
            } else {
                this.title = '详细信息';
                this.maxHeight = '100%';
            }
            // 清空数据
            this.$refs.form.resetFields();
            if (this.type == '0' || this.type == '1') {
                // 回显数据处理
                this.form = this.data;
            } else {
                // 添加
                delete this.form.id;
            }
            this.visible = value;
        },
        getTypeDataList() {
            // 学历层次
            // getDictDataByTypePost({type:"where-manage-type", version: this.form.version}).then(res=>{
            //   if (res.success) {
            //     this.typeList = res.result;
            //   }
            // })
            getDictDataByType('where-manage-type').then(res => {
                this.treeLevelLoading = false;
                if (res.success) {
                    this.treeData = res.result;
                } else {
                    this.treeData = null;
                }
            });
        },
        onRestWindow() {
            this.labelWidth100 = this.searchLabelWidth(this.labelWidth100)
        }
    },
    watch: {
        data(val) {
            this.setCurrentValue(val);
        },
        visible(value) {
            this.$emit('input', value);
        }
    },
    mounted() {
        this.onRestWindow()
        window.addEventListener('resize',this.onRestWindow)
        this.init();
    },
    beforeDestroy() {
        window.removeEventListener('resize',this.onRestWindow)
    },
};
</script>

<style lang="less">
// 建议引入通用样式 具体路径自行修改 可删除下面样式代码
// @import "../../../styles/drawer-common.less";
@import '../../personInfo.less';
// 建议引入通用样式 具体路径自行修改 可删除下面样式代码
@import '../../../styles/table-common.less';

l .drawer-footer {
    z-index: 10;
    width: 100%;
    position: absolute;
    bottom: 0;
    left: 0;
    border-top: 1px solid #e8e8e8;
    padding: 10px 16px;
    text-align: left;
    background: #fff;
}

.drawer-content {
    overflow: auto;
}

.drawer-content::-webkit-scrollbar {
    display: none;
}

.drawer-header {
    display: flex;
    align-items: center;
    margin-bottom: 16px;
    font-size: 16px;
    color: rgba(0, 0, 0, 0.85);
}

.drawer-title {
    font-size: 16px;
    color: rgba(0, 0, 0, 0.85);
    display: block;
    margin-bottom: 16px;
}
</style>
<style lang="less" scoped>
.cmr16{
    margin-right: 16px
}
.cmr10 {
    margin-right: 10px 
}

</style>